// @vitest-environment jsdom
//
// The [locale] mirror pages re-export the bare page and add getStaticPaths,
// which tells `voltro build` which locale-prefixed paths to emit. The default
// locale (en) is NOT listed — it lives at the bare path — so only /de is
// produced. The dynamic post mirror emits the CROSS-PRODUCT of non-default
// locales × post slugs (here: de × every post).

import { describe, expect, test, vi } from 'vitest'
import type { ComponentType } from 'react'
import { posts } from '../../content/posts'

// The mirrors transitively import the base pages, which import @voltro/web
// (island / loader / notFound / useLocation via lib/locale) and @voltro/i18n.
// Stub both so importing the modules doesn't require the real runtimes.
vi.mock('@voltro/web', () => ({
  useLoaderData: () => ({}),
  notFound: (detail?: string) => {
    throw new Error(detail)
  },
  island: (Component: ComponentType<unknown>) => Component,
  useLocation: () => '/',
}))
vi.mock('@voltro/i18n', () => ({
  defineCatalog: <T,>(c: T): T => c,
  defineLocale:
    () =>
    <T,>(c: T): T =>
      c,
  useLocale: () => 'en',
  useT: (id: string) => id,
  T: ({ id }: { id: string }) => id,
}))

const indexMirror = await import('./page')
const postMirror = await import('./blog/[slug]/page')

describe('home mirror — getStaticPaths', () => {
  test('emits only non-default locales (just /de)', async () => {
    const paths = await indexMirror.getStaticPaths()
    expect(paths).toEqual([{ params: { locale: 'de' } }])
  })

  test('re-exports the base page component + its meta function', () => {
    expect(typeof indexMirror.default).toBe('function')
    expect(indexMirror.meta({ locale: 'de' }).title).toContain('Blog')
  })
})

describe('post mirror — getStaticPaths (locale × slug cross-product)', () => {
  test('emits one path per non-default locale × post slug', async () => {
    const paths = await postMirror.getStaticPaths()
    expect(paths).toEqual(posts.map((post) => ({ params: { locale: 'de', slug: post.slug } })))
  })

  test('re-exports the base post page + its config exports', () => {
    expect(typeof postMirror.default).toBe('function')
    expect(postMirror.renderMode).toBe('static')
    expect(postMirror.interactive).toBe('islands')
    expect(typeof postMirror.loader).toBe('function')
  })
})
